<!--  -->
<template>
  <div class="main">
    <h2 style="margin-left:15px">对账文件下载</h2>
    <div id="search-card">
      <div id="search-content">
        <el-form v-model="search" :inline="true">

          <el-form-item label="日期">
            <el-date-picker
              v-model="search.date"
              type="date"
              placeholder="选择日期"
            />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" style="margin-left:10px" @click="mohuQuery">查找</el-button>
            <el-button icon="el-icon-refresh" circle @click="refresh()" />
          </el-form-item>
        </el-form>
      </div>
    </div>

    <div id="table-area">
      <el-table
        v-loading="listLoading"
        :data="list.slice((currentPage-1)*pagesize,currentPage*pagesize)"
        element-loading-text="Loading"
        border
        fit
        highlight-current-row
      >

        <el-table-column
          label="序号"
          type="index"
          align="center"
        >
          <template scope="scope">
            <span>{{ (currentPage - 1) * pagesize + scope.$index + 1 }}</span>
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          prop="name"
          label="交易对账/清算对账名"
        />

        <el-table-column
          align="center"
          prop="updateTime"
          label="更新时间"
        />

        <el-table-column align="center" label="操作" width="200px">
          <template slot-scope="Scope">
            <el-button type="primary" @click="download(Scope.row.download,Scope.row.name)">下载</el-button>
          </template>
        </el-table-column>

      </el-table>

      <div id="pages">
        <el-pagination
          :current-page="currentPage"
          :page-sizes="[5,10]"
          :page-size="pagesize"
          layout="total, sizes,prev, pager, next"
          :total="list.length"
          prev-text="上一页"
          next-text="下一页"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>

  </div>
</template>

<script>
// 这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
// 例如：import 《组件名称》 from '《组件路径》';
import { selectAll, selectAllByTime } from '@/api/duizhang'
export default {
  name: 'Orders',
  // import引入的组件需要注入到对象中才能使用
  components: {},
  data() {
    // 这里存放数据
    return {
      currentPage: 1, // 默认显示页面为1
      pagesize: 5, //    每页的数据条数
      list: [],
      lesson: [],
      search: {},
      leName: '',
      mohu: '',
      listLoading: false,
      order: {},
      isUse: false,
      date: ''
    }
  },
  // 监听属性 类似于data概念
  computed: {},
  // 监控data中的数据变化
  watch: {},
  // 生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.fetchData()
  },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, // 生命周期 - 创建之前
  beforeMount() {}, // 生命周期 - 挂载之前
  beforeUpdate() {}, // 生命周期 - 更新之前
  updated() {}, // 生命周期 - 更新之后
  beforeDestroy() {}, // 生命周期 - 销毁之前
  destroyed() {}, // 生命周期 - 销毁完成
  activated() {},
  // 方法集合
  methods: {
    handleSizeChange: function(size) {
      this.pagesize = size
    },
    // 点击第几页
    handleCurrentChange: function(currentPage) {
      this.currentPage = currentPage
    },
    handleClick(row) {
      console.log(row)
    },
    fetchData() {
      selectAll().then((resp) => {
        this.list = resp.obj
      })
      this.listLoading = false
    },
    refresh() {
      this.search = {}
      this.fetchData()
    },
    download(url, name) {
      const aLink = document.createElement('a')
      aLink.href = url// 设置下载的图片链接
      aLink.download = name// 设置图片下载之后的名称
      aLink.click()// 触发点击事件
    },
    mohuQuery() {
      if (this.search.date != null) {
        console.log(this.getFormatDate(this.search.date))
        selectAllByTime(this.getFormatDate(this.search.date)).then((resp) => {
          this.list = resp.obj
        })
      } else {
        console.log('搜索内容为空')
      }
    },
    getFormatDate(dat) {
      const date = dat
      let month = '' + (date.getMonth() + 1)
      let day = '' + date.getDate()
      const year = date.getFullYear()
      if (month.length < 2) month = '0' + month
      if (day.length < 2) day = '0' + day
      return [year, month, day].join('-')
    }
  }
}
</script>
<style  scoped>
#main{
  margin-left: 5px;
  margin-right: 5px;
}
#table-area{
  margin-left: 10px;
  margin-right: 10px;
}
#search-card {
  line-height: 0px;
  text-align: right;
  width: 100%;
  height: 70px;
  overflow-x: hidden;
}
</style>
